Importing Packages


In [4]:
from scipy.optimize import curve_fit
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sympy as sym

Function to perform curve fitting on every state


In [2]:
def read_and_clean_csv(csvfile):
    df = pd.read_csv(csvfile)
    df.drop('Unnamed: 0',axis=1, inplace = True)
    return df

def func(x ,a ,b ,c, d):
    return a*x**3 + b*x**2 +c*x + d

def plot_curve_fitting(function, params, xdata, ydata, state):
    plt.figure(figsize=(12, 8))
    plt.scatter(xdata ,ydata ,alpha=0.6 ,label='Data', color = 'red')
    plt.ylim(1,100)
    plt.plot(xdata,func(xdata, params[0], params[1], params[2], params[3]),
         label='Fitted function', linewidth = 2.5)
    plt.xlabel('Age')
    plt.ylabel('Probability of Reconstruction')
    plt.legend(loc='best')
    plt.title("Curve Fitting\n State: "+ str(state)+'\n'+\
              '\n a: '+str(params[0])+' b: '+str(params[1])+' c: '+str(params[2])+ ' d: '+str(params[3]))
              
    return plt.show()

def main(csvfile):
    #csvfiles = ['cprsmidnortheast.csv','cprssouthwest.csv','cprssouthwest.csv','cprsmidwest.csv','cprswest.csv'] 
    df = read_and_clean_csv(csvfile)
    for state in df.drop('Age', axis = 1):
        xdata = np.array(np.arange(1,61))
        ydata = np.array(df[state])
        params, params_covariance = curve_fit(func, xdata, ydata)
        plot_curve_fitting(func, params, xdata, ydata, state)

Curve Fitting (Midwest)


In [5]:
main('cprsmidwest.csv')


WEST


In [6]:
main('cprswest.csv')


SOUTH WEST


In [8]:
main('cprsouthwest.csv')


SOUTH EAST


In [11]:
main('cprsoutheast.csv')


NORTH EAST


In [10]:
main('cprsmidnortheast.csv')



In [ ]: